package tech.aistar.day09;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 本类用来演示: 关于集合的删除
 *
 * @author: success
 * @date: 2022/7/28 9:35 上午
 */
public class ArrayListRemoveDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");

        //modCount = 3;

        //for (String s : list) { // 只读的 - 增强for循环就是迭代器
            //if("b".equals(s)){
            //    list.remove("b");
            //}
            // add remove方法底层modCount都会自增1
            // 迭代器-> int expectedModCount = modCount;
            //list.remove("b");//报错
       // }
       // System.out.println(list);

        //Iterator<String> iter = list.iterator();
        //while(iter.hasNext()){
        //    String s = iter.next();
        //
        //    //first->虽然不会报错,modCount已经变成4了
        //    //list.remove(s);//报错
        //
        //    if("b".equals(s)){
        //        list.remove(s);
        //    }
        //}
        //System.out.println(list);

        //Iterator<String> iterator = list.iterator();
        //while(iterator.hasNext()){
        //    String s = iterator.next();
        //    if(s.equals("a")){
        //        iterator.remove();
        //    }
        //}
        //System.out.println(list);

        //迭代器遍历的时候,不能够遍历2次
        Iterator<String> iterator = list.iterator();
        while(iterator.hasNext()){
            String s = iterator.next();
            System.out.println(s);
        }

        //while(iterator.hasNext()){
        //    String s = iterator.next();
        //    System.out.println(s);
        //}


        //删除多个
        List<Book> books = new ArrayList<>();
        Book b1 = new Book(1,"python",100.0d);
        Book b2 = new Book(2,"javascript",100.0d);
        Book b3 = new Book(3,"i love java",200.0d);
        Book b4 = new Book(4,"java",300.0d);
        Book b5 = new Book(5,"mysql",100.0d);

        books.add(b1);
        books.add(b2);
        books.add(b3);
        books.add(b4);
        books.add(b5);

        delLikeName(books,"java");

        books.forEach(e-> System.out.println(e));
    }

    private static void delLikeName(List<Book> books, String name) {
        //普通for循环
        if(null!=books && books.size()>0){
            /*
            for (int i = 0; i < books.size(); i++) {
                //boolean indexOf(E e);//判断该元素在集合中第一次出现的下标的位置
                //如果没有出现过,则返回-1
                if(books.get(i).getBookName().indexOf(name)!=-1){
                    books.remove(i);//根据下标进行删除
                    i--;
                }
            }*/

            //for (int i = books.size()-1; i>=0; i--) {
            //    if(books.get(i).getBookName().indexOf(name)!=-1)
            //        books.remove(i);
            //}

            List<Book> delBooks = new ArrayList<>();

            for (Book book : books) {
                if(book.getBookName().contains(name)){
                    delBooks.add(book);
                }
            }

            books.removeAll(delBooks);
        }
    }
}
